package org.elanfox.intellicommunity.intellicommunityadmin.service.impl;

import org.elanfox.intellicommunity.biz.entity.equipment.BaseStationPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.BaseStationService;
import org.elanfox.intellicommunity.common.constant.RedisConstant;
import org.elanfox.intellicommunity.intellicommunityadmin.mq.MsgProvider;
import org.elanfox.intellicommunity.intellicommunityadmin.service.CmdSendService;
import org.elanfox.intellicommunity.msg.dto.*;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/19
 */
@Service
public class CmdSendServiceImpl implements CmdSendService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private MsgProvider msgProvider;
    @Autowired
    private BaseStationService baseStationService;

    private String redisKey(String hexAddr) {
        return RedisConstant.KEY_CMD + hexAddr;
    }

    @Override
    public void lfScope(Integer stationId, int scope) {
        BaseStationPO baseStationPO = baseStationService.findById(stationId);
        //创建指令
        CmdLfScopeWriteParam writeParam = new CmdLfScopeWriteParam();
        writeParam.setStationType(baseStationPO.getStationType());
        writeParam.setScope(scope);
        writeParam.setDeviceAddr(baseStationPO.getDeviceAddr());

        CmdDTO cmdDTO = new CmdDTO(CmdEnums.Code.LF_SCOPE, writeParam, stationId);

        redisTemplate.opsForHash().put(redisKey(writeParam.getHexAddr()), CmdEnums.Code.LF_SCOPE.getCode(), cmdDTO);

        //发送指令
        msgProvider.sendCommandMsg(cmdDTO);
    }

    @Override
    public void rssiFilter(Integer stationId, boolean enabled, int limitVal) {
        BaseStationPO baseStationPO = baseStationService.findById(stationId);

        //创建指令
        CmdRssiFilterWriteParam writeParam = new CmdRssiFilterWriteParam();
        writeParam.setDeviceAddr(baseStationPO.getDeviceAddr());
        writeParam.setStationType(baseStationPO.getStationType());
        writeParam.setEnabled(enabled);
        writeParam.setLimitVal(limitVal);
        CmdDTO cmdDTO = new CmdDTO(CmdEnums.Code.RSSI_FILTER, writeParam, stationId);

        redisTemplate.opsForHash().put(redisKey(writeParam.getHexAddr()), CmdEnums.Code.RSSI_FILTER.getCode(), cmdDTO);
        //发送指令
        msgProvider.sendCommandMsg(cmdDTO);
    }

    @Override
    public void buzzerStatus(Integer stationId, boolean enabled) {
        BaseStationPO baseStationPO = baseStationService.findById(stationId);

        CmdBuzzerStatusWriteParam writeParam = new CmdBuzzerStatusWriteParam(baseStationPO.getStationType(), baseStationPO.getDeviceAddr(), enabled);
        CmdDTO cmdDTO = new CmdDTO(CmdEnums.Code.BUZZER_STATUS, writeParam, stationId);

        redisTemplate.opsForHash().put(redisKey(writeParam.getHexAddr()), CmdEnums.Code.BUZZER_STATUS.getCode(), cmdDTO);
        //发送指令
        msgProvider.sendCommandMsg(cmdDTO);
    }

    @Override
    public void deviceAddr(Integer stationId, long deviceAddr) {
        BaseStationPO baseStationPO = baseStationService.findById(stationId);

        CmdAddrWriteParam writeParam = new CmdAddrWriteParam(baseStationPO.getStationType(), baseStationPO.getDeviceAddr(), deviceAddr);

        CmdDTO cmdDTO = new CmdDTO(CmdEnums.Code.DEVICE_ADDR, writeParam, stationId);

        redisTemplate.opsForHash().put(redisKey(writeParam.getHexAddr()), CmdEnums.Code.DEVICE_ADDR.getCode(), cmdDTO);
        //发送指令
        msgProvider.sendCommandMsg(cmdDTO);
    }

    @Override
    public void filterTime(Integer stationId, int interval) {
        BaseStationPO baseStationPO = baseStationService.findById(stationId);

        CmdFilterWriteParam writeParam = new CmdFilterWriteParam(baseStationPO.getStationType(), baseStationPO.getDeviceAddr(), interval);

        CmdDTO cmdDTO = new CmdDTO(CmdEnums.Code.FILTER_WAITING_TIME, writeParam, stationId);

        redisTemplate.opsForHash().put(redisKey(writeParam.getHexAddr()), CmdEnums.Code.FILTER_WAITING_TIME.getCode(), cmdDTO);
        //发送指令
        msgProvider.sendCommandMsg(cmdDTO);
    }
}
